package com.albert.order.service;

import com.albert.order.dao.OrderInfoMapper;
import com.albert.order.delayqueue.DelayQueueProcessor;
import com.albert.order.entry.DelayOrderTask;
import com.albert.order.entry.OrderInfo;
import com.albert.order.entry.OrderTypeEnum;
import com.albert.order.mq.MqProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
 * 限时订单服务类
 *
 * @author yangjunwei
 * @date 2021/7/3 上午1:46
 */
@Service
@Slf4j
public class DelayOrderService {

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    MqProducer mqProducer;

    @Autowired
    DelayQueueProcessor delayQueueProcessor;

    @Autowired
    OrderInfoMapper orderInfoMapper;

    public void createDelayQueue(OrderInfo orderInfo) {
        if (Objects.isNull(orderInfo)) {
            return;
        }
        orderInfo.setCreateTime(new Date());
        //1.添加订单信息到数据库
        orderInfoService.insert(orderInfo);
        log.info("-----------------create order------------- 新增订单：{}", orderInfo);
        //2.添加订单到延时队列
        delayQueueProcessor.orderDelay(orderInfo);
    }

    public void sendToActivemq(OrderInfo orderInfo) {
        if (Objects.isNull(orderInfo)) {
            return;
        }
        orderInfo.setCreateTime(new Date());
        //1.添加订单信息到数据库
        orderInfoService.insert(orderInfo);
        log.info("-----------------create order------------- 新增订单：{}", orderInfo);
        Long timeLimitValue = OrderTypeEnum.getTimeLimitValueByOrderType(orderInfo.getOrderType());

        //2.发送订单信息到activemq
        mqProducer.scheduleSnedMsg(orderInfo.getId(), timeLimitValue);
    }


    public void fillDelayOrder(Integer orderId) {
    }
}
